# from app.models import User
# from app import db
#
#
# def register_user(username, password):
#     user = User(username=username)
#     user.set_password(password)
#     db.session.add(user)
#     db.session.commit()
#     return user
# app/services/user_service.py
from app.models.user import User
import re

# import pymysql
# from flask_sqlalchemy import SQLAlchemy
# pymysql.install_as_MySQLdb()
# db = SQLAlchemy()

class UserService:
    def __init__(self):
        # 在这里可以初始化用户数据，比如从文件中加载用户信息
        self.users = User()

    def find_user_by_username(self, username, password):
        print(self.users.username)
        # 验证账号密码

        user = self.users.query.filter_by(username=username, password=password).first()
        print('查询：', user)
        # if username in self.users:
        #     return User(username, self.users[username])
        if user:
            return user
        else:
            return None
        # return 'fdfa'

    def is_valid_phone_number(self,phone_number):
        # 使用正则表达式来匹配美国电话号码的格式
        pattern = re.compile(r"^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$")
        return bool(pattern.match(phone_number))
